package com.iretailer.report.util;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: keepx
 * @Date: 2018/10/18 22:15
 * @Description: 1000 表示 成功  1001 未知异常  1002 有关联不能删除
 */
public class RMap extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    public enum Code {
        OK(1000), // 成功
        NOFOUND(404), //没有
        UNKNOWN(1001), //未知异常
        CONNECTED(1002), //有关联关系,
        DATAREPETITION(1003);//数据重复错误
        private int code;

        private Code(int code) {
            this.code = code;
        }

        public int value() {
            return this.code;
        }
    }

    public RMap() {
        put("status", Code.OK.value());
    }

    public static RMap resultBoolean(boolean result) {
        if (result) {
            return RMap.ok(true);
        } else {
            return RMap.error(false);
        }
    }

    public static RMap error() {
        return error(Code.UNKNOWN, "unknown_exception_please_contact_administrator");
    }

    public static RMap errorNotDeleted() {
        return error("associations_cannot_be_deleted");
    }

    public static RMap error(Object msg) {
        return error(Code.UNKNOWN, msg);
    }

    public static RMap error(RMap.Code code, Object msg) {
        RMap r = new RMap();
        r.put("status", code.value());
        r.put("showText", msg);
        return r;
    }


    public static RMap ok(Object msg) {
        RMap r = new RMap();
        r.put("data", msg);
        return r;
    }

//    public static RMap ok(Map<String, Object> map) {
//        RMap r = new RMap();
//        r.putAll(map);
//        return r;
//    }

    public static RMap ok() {
        return new RMap();
    }


    @Override
    public RMap put(String key, Object value) {
        super.put(key, value);
        return this;
    }
}
